package aquarium.model;


import aquarium.model.common.AquariumAppModule;
import aquarium.model.view.FishDataViewImpl;
import aquarium.model.view.FishDataViewRowImpl;
import aquarium.model.view.FishOccurrenceViewImpl;
import aquarium.model.view.FishOccurrenceViewRowImpl;
import aquarium.model.view.FishPhotosViewImpl;
import aquarium.model.view.FishPhotosViewRowImpl;

import aquarium.model.view.SimmilarFishesImpl;

import java.sql.CallableStatement;
import java.sql.SQLException;

import oracle.adf.share.logging.ADFLogger;

import oracle.jbo.JboException;
import oracle.jbo.ViewCriteria;
import oracle.jbo.domain.Number;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;

import oracle.ord.im.OrdImageDomain;


// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Wed Dec 12 20:18:32 CET 2012
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class AquariumAppModuleImpl extends ApplicationModuleImpl implements AquariumAppModule {

    private static final ADFLogger LOG = ADFLogger.createADFLogger("AquariumAppModuleImpl");

    /**
     * This is the default constructor (do not remove).
     */
    public AquariumAppModuleImpl() {
    }

    /**
     * Container's getter for FishDataViewAll.
     * @return FishDataViewAll
     */
    public FishDataViewImpl getFishDataViewAll() {
        return (FishDataViewImpl)findViewObject("FishDataViewAll");
    }

    /**
     * Container's getter for FishPhotosViewAll.
     * @return FishPhotosViewAll
     */
    public FishPhotosViewImpl getFishPhotosViewAll() {
        return (FishPhotosViewImpl)findViewObject("FishPhotosViewAll");
    }

    /**
     * Container's getter for FishOccurrenceViewAll.
     * @return FishOccurrenceViewAll
     */
    public FishOccurrenceViewImpl getFishOccurrenceViewAll() {
        return (FishOccurrenceViewImpl)findViewObject("FishOccurrenceViewAll");
    }

    /**
     * Container's getter for FishPhotosViewForFish.
     * @return FishPhotosViewForFish
     */
    public FishPhotosViewImpl getFishPhotosViewForFish() {
        return (FishPhotosViewImpl)findViewObject("FishPhotosViewForFish");
    }

    /**
     * Container's getter for FishPhotosFishOccurenceFk1Link1.
     * @return FishPhotosFishOccurenceFk1Link1
     */
    public ViewLinkImpl getFishPhotosFishOccurenceFk1Link1() {
        return (ViewLinkImpl)findViewLink("FishPhotosFishOccurenceFk1Link1");
    }

    /**
     * Container's getter for FishPhotosFishDataFk1Link1.
     * @return FishPhotosFishDataFk1Link1
     */
    public ViewLinkImpl getFishPhotosFishDataFk1Link1() {
        return (ViewLinkImpl)findViewLink("FishPhotosFishDataFk1Link1");
    }


    public void createFishDataWithOccurr() {

        FishDataViewRowImpl fRow = (FishDataViewRowImpl)getFishDataViewAll().createRow();
        getFishDataViewAll().insertRow(fRow);
        getFishDataViewAll().setCurrentRow(fRow);
        FishOccurrenceViewRowImpl oRow = (FishOccurrenceViewRowImpl)fRow.getFishOccurrenceView().createRow();
        fRow.getFishOccurrenceView().insertRow(oRow);
    }
    private static final String CONTENT_TYPE = "image/jpeg; charset=utf-8";

    public Number addFishPhoto(byte[] bytes, String fileName) {
        OrdImageDomain fishFile = new OrdImageDomain(bytes);
        try {
            fishFile.setContentFormat(CONTENT_TYPE);
            fishFile.setMimeType(CONTENT_TYPE);
        } catch (SQLException e) {
            throw new JboException(e);
        }
        FishDataViewRowImpl fRow = (FishDataViewRowImpl)getFishDataViewAll().getCurrentRow();
        FishPhotosViewRowImpl pRow = (FishPhotosViewRowImpl)fRow.getFishPhotosView().createRow();
        LOG.fine("Inserting row with id " + pRow.getFpIdPicture());
        pRow.setFpImage(fishFile);
        pRow.setFpFileName(fileName);

        return pRow.getFpIdPicture();

    }

    public void updateStillImage(Number rowId) {
        CallableStatement cs = null;

        String sql = "begin updateStillImage(:row_id); end;";
        try {

            cs = getDBTransaction().createCallableStatement(sql, this.getDBTransaction().DEFAULT);
            LOG.fine("Looking for row with id " + rowId);
            cs.setObject("row_id", rowId);
            cs.execute();
        } catch (SQLException e) {
            throw new JboException(e);
        } finally {
            if (cs != null) {
                try {
                    cs.close();
                } catch (SQLException e) {
                    LOG.warning(e);
                }
            }
        }
    }

    /**
     * Container's getter for SimmilarFishesAll.
     * @return SimmilarFishesAll
     */
    public SimmilarFishesImpl getSimmilarFishesAll() {
        return (SimmilarFishesImpl)findViewObject("SimmilarFishesAll");
    }


    public void searchSimmilarFishes(byte[] bytes) {

        OrdImageDomain fishFile = new OrdImageDomain(bytes);
        try {
            fishFile.setContentFormat(CONTENT_TYPE);
            fishFile.setMimeType(CONTENT_TYPE);
        } catch (SQLException e) {
            throw new JboException(e);
        }
        FishDataViewRowImpl fRow = (FishDataViewRowImpl)getFishDataViewAll().createRow();
        FishPhotosViewRowImpl pRow = (FishPhotosViewRowImpl)fRow.getFishPhotosView().createRow();
    
        LOG.fine("Inserting row with id " + pRow.getFpIdPicture());
        pRow.setFpImage(fishFile);
        getDBTransaction().commit();

        updateStillImage(pRow.getFpIdPicture());

        getDBTransaction().commit();
        applyCriteriaForSearch(pRow.getFpIdPicture());
        
        fRow.remove();
        getDBTransaction().commit();
        
    }

    private void applyCriteriaForSearch(Number rowId) {
        getSimmilarFishesAll().applySearchCriteria(rowId);
    }
}
